package com.waimai.app.qa.support;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

/**
* 远程Shell脚本执行工具
* @author AiHui
*/
public class RmtShellExecutor {
	
	private Connection conn;
	
	//远程机器IP 
	private String ipAddr;
	
	private String charset = Charset.defaultCharset().toString();
	
	//用户名  
	private String userName;
	
	//密码 
	private String password;
	
	//字符集 
	private File keyfile;
	
	public RmtShellExecutor(String ipAddr, String userName, String password, File keyfile) {
	   this.ipAddr = ipAddr;
	   this.userName = userName;
	   this.password = password;
	   this.keyfile = keyfile;
	   if(charset != null) {
		    this.charset = charset;
	   }
	}

	/** *//**  
	 * 构造函数 
	 */ 
	public Connection getConn() {
		return conn;
	}
	public void setConn(Connection conn) {
		this.conn = conn;
	}
	
	public String getIpAddr() {
		return ipAddr;
	}
	public void setIpAddr(String ipAddr) {
		this.ipAddr = ipAddr;
	}
	
	public String getCharset() {
		return charset;
	}
	public void setCharset(String charset) {
		this.charset = charset;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public File getFile() {
		return keyfile;
	}
	public void setFile(File keyfile) {
		this.keyfile = keyfile;
	}

	/**   
	 * 登录   
	 * @return  
	 * @throws IOException  
	 */ 
	public boolean login() throws IOException {
	   conn = new Connection(ipAddr);
	   conn.connect(); //连接
	   return conn.authenticateWithPublicKey(userName,keyfile,password); //本地RSA文件认证
	}
	
	/**   
	 * 执行脚本   
	 * @param cmds  
	 * @return  
	 * @throws Exception  
	 */ 
	public String exec(String cmds) {
	   InputStream in = null;
	   String result = "";
	   try {
	    if (this.login()) {
	    	Session session = conn.openSession(); //打开一个会话
	    	session.execCommand(cmds);
	    	in = session.getStdout();
	    	result = this.processStdout(in, this.charset);
	    	conn.close();
	    }
	   } catch (IOException e1) {
		   e1.printStackTrace();
	   }
	   return result;
	}
	
	/**
	 * 解析流获取字符串信息
	 * @param in 输入流对象
	 * @param charset 字符集
	 * @return  
	 * @throws IOException  
	 * @throws UnsupportedEncodingException  
	 */ 
	public String processStdout(InputStream in, String charset) {
	   byte[] buf = new byte[1024];
	   StringBuffer sb = new StringBuffer();
	   try {
	    while (in.read(buf) != -1) {
	    	sb.append(new String(buf, charset));
	    }
	   } catch (IOException e) {
		   e.printStackTrace();
	   }
	   return sb.toString();
	}

	public void rmtCallShell(String keyFilePath, String ip, String userName, String password, String shell){
		try{
			File keyfile = new File(keyFilePath);
			RmtShellExecutor test = new RmtShellExecutor(ip,userName,password,keyfile);
			test.exec(shell);
			//System.out.println(test.exec(shell));
		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

/*public static void main(String args[]){
	try{
		File keyfile = new File("D:\\id_rsa");
		RmtShellExecutor test = new RmtShellExecutor("10.0.31.46", "root", "sugon123",keyfile);
		System.out.println(test.exec("sh /opt/e.sh "));
	}catch (Exception ex) {
		ex.printStackTrace();
	}
 }
}*/


